package com.itany.corejava.code09_集合.collections;

import java.util.*;

/**
 * @author MissChen
 * @version 1.0
 * @date 2023 年11月22日 14:33
 */
public class Test02_练习 {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        List<Stu> list=new ArrayList<>();
        for(int i=1;i<=5;i++){
            System.out.print("请输入学生的姓名:");
            String name=sc.next();
            System.out.print("请输入学生的分数:");
            double score=sc.nextDouble();
            Stu stu=new Stu(name,score);
            list.add(stu);

        }
        Collections.sort(list, new Comparator<Stu>() {
            @Override
            public int compare(Stu stu1, Stu stu2) {
                if(stu1.getScore()>stu2.getScore()){
                    return -1;
                } else if (stu1.getScore()<stu2.getScore()) {
                    return 1;
                }
                return 0;
            }
        });

        System.out.println(list);
    }


    /*定义一个学生类
    有name、score属性
    循环添加5个学生，将其存入到ArrayList中去
    在控制台按该学生的从高到低排列，在控制台输出*/


}
class Stu{
    private String name;
    private double score;

    public Stu(String name, double score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Stu{" +
                "name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
}
